* This code estimates the placebo results
* in the paper Espadafor & Martínez 2021
* using NUMBER OF STUDENTS as main outcome *

*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"

use "gambling_data.dta", clear
merge m:1 apuestas_id using "dapuestas_year.dta"

/***********************
***********************

** Variables:

* Not used:

gen t=0
replace t=1 if year==2014 & publico==2 & year_lic==2013 & metres<500
replace t=1 if year==2015 & publico==2 & year_lic==2013 & metres<500
replace t=1 if year==2015 & publico==2 & year_lic==2014 & metres<500
replace t=1 if year==2015 & publico==2 & year_lic==2013 & metres<500
replace t=1 if year==2016 & publico==2 & year_lic==2013 & metres<500
replace t=1 if year==2016 & publico==2 & year_lic==2014 & metres<500
replace t=1 if year==2016 & publico==2 & year_lic==2015 & metres<500
replace t=1 if year==2017 & publico==2 & year_lic==2013 & metres<500
replace t=1 if year==2017 & publico==2 & year_lic==2014 & metres<500
replace t=1 if year==2017 & publico==2 & year_lic==2015 & metres<500
replace t=1 if year==2017 & publico==2 & year_lic==2016 & metres<500
replace t=1 if year==2017 & publico==2 & year_lic==2017 & metres<500

*/

gen treatment=0
replace treatment=1 if metres<500 & year>=year_lic & publico==2 

gen treatment_all=0
replace treatment_all=1 if metres<500 & year>=year_lic


* This is to manually get data 'wide'

drop num
gen num=.
replace num= n18 if year==2017
replace num= n16 if year==2016
replace num= n15 if year==2015
replace num= n14 if year==2014

drop pau
gen pau=.
replace pau= pau1516 if year==2015
replace pau= pau16 if year==2016
replace pau= pau17 if year==2017
replace pau= pau14 if year==2014

* Income by district: 
drop renta
gen renta = .
replace renta = 31 if distrito==4
replace renta = 40 if distrito==1
replace renta = 51 if distrito==14
replace renta = 53 if distrito==15
replace renta = 60 if distrito==5
replace renta = 34 if distrito==17
replace renta = 48 if distrito==6
replace renta = 48 if distrito==8
replace renta = 56 if distrito==11
replace renta = 30 if distrito==10
replace renta = 27 if distrito==3
replace renta = 25 if distrito==18
replace renta = 24 if distrito==13
replace renta = 34 if distrito==12
replace renta = 36 if distrito==7
replace renta = 47 if distrito==9
replace renta = 26 if distrito==21
replace renta = 30 if distrito==20
replace renta = 31 if distrito==19
replace renta = 34 if distrito==16
replace renta = 46 if distrito==2

* Dummy variable for income: Districts with incomes higher to the median==1
drop renta2
gen renta2 = 0 
replace renta2 = 1 if renta>40

/* Variables and placebos about the treatment:

gen treatment_all=0
replace treatment_all=1 if metres<500 & year>year_lic

gen treatment_pl=0
replace treatment_pl=1 if metres<500 & year==year_lic

gen treatment_pl2=0
replace treatment_pl2=1 if metres<500 & year<year_lic

gen treatment_pl3=0
replace treatment_pl3=1 if metres<500 & year<year_lic-1

gen treatmentfut = 0
replace treatmentfut = 1 if metres<500 & year>year_lic+1

*/

*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"


** Publico=1 -> charter schools
** Publico=2 -> public schools

** Renta2=0 -> poorer 
** Renta2=1 -> richer

*use workingfile.dta, clear

drop treat
gen treat=treatment_all

***********************
***********************

*** This makes stata understand data as a panel. And, when applying the xtreg command, adding fixed effects at the observation level. 

xtset school_id year

***********************
***********************

xtreg num treat i.year, fe
regsave treat using "num0.dta", tstat pval ci level(90) replace addlabel (Outcome, num,Income, All, School, All, Year, 1, Level, 90)

xtreg num treat i.year, fe
regsave treat using "num0.dta", tstat pval ci level(95) append addlabel (Outcome, num,Income, All, School, All, Year, 1, Level, 95)


*** Charter

xtreg num treat i.year if publico==1, fe
regsave treat using "num0.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, All, School, Charter, Year, 1, Level, 90)

xtreg num treat i.year if publico==1, fe
regsave treat using "num0.dta", tstat pval ci level(95) append addlabel (Outcome, num,Income, All, School, Charter, Year, 1, Level, 95)

***Public

xtreg num treat i.year if publico==2, fe
regsave treat using "num0.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, All, School, Public, Year, 1, Level, 90)

xtreg num treat i.year if publico==2, fe
regsave treat using "num0.dta", tstat pval ci level(95) append  addlabel (Outcome, num,Income, All, School, Public, Year, 1, Level, 95)

*** Charter, poor

xtreg num treat i.year if publico==1 & renta2==0, fe
regsave treat using "num0.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, Poor, School, Charter, Year, 1, Level, 90)

xtreg num treat i.year if publico==1 & renta2==0, fe
regsave treat using "num0.dta", tstat pval ci level(95) append addlabel (Outcome, num,Income, Poor, School, Charter, Year, 1, Level, 95)

*** Charter, rich

*xtreg num treat i.year if publico==1 & renta2==1, fe
*regsave treat using "num0.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, Rich, School, Charter, Year, 1, Level, 90)

*xtreg num treat i.year if publico==1 & renta2==1, fe
*regsave treat using "num0.dta", tstat pval ci level(95) append addlabel (Outcome, num,Income, Rich, School, Charter, Year, 1, Level, 95)

*** Public, poor

xtreg num treat i.year if publico==2 & renta2==0, fe
regsave treat using "num0.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, Poor, School, Public, Year, 1, Level, 90)

xtreg num treat i.year if publico==2 & renta2==0, fe
regsave treat using "num0.dta", tstat pval ci level(95) append addlabel (Outcome, num, Income, Poor, School, Public, Year, 1, Level, 95)

*** Public, rich

xtreg num treat i.year if publico==2 & renta2==1, fe
regsave treat using "num0.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, Rich, School, Public, Year, 1, Level, 90)

xtreg num treat i.year if publico==2 & renta2==1, fe
regsave treat using "num0.dta", tstat pval ci level(95) append addlabel (Outcome, num, Income, Rich, School, Public, Year, 1, Level, 95)

**************************************************
**************************************************

*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data"

use "gambling_data.dta", clear
merge m:1 apuestas_id using "dapuestas_year.dta"

/***********************
***********************

** Variables:

* Not used:

gen t=0
replace t=1 if year==2014 & publico==2 & year_lic==2013 & metres<500
replace t=1 if year==2015 & publico==2 & year_lic==2013 & metres<500
replace t=1 if year==2015 & publico==2 & year_lic==2014 & metres<500
replace t=1 if year==2015 & publico==2 & year_lic==2013 & metres<500
replace t=1 if year==2016 & publico==2 & year_lic==2013 & metres<500
replace t=1 if year==2016 & publico==2 & year_lic==2014 & metres<500
replace t=1 if year==2016 & publico==2 & year_lic==2015 & metres<500
replace t=1 if year==2017 & publico==2 & year_lic==2013 & metres<500
replace t=1 if year==2017 & publico==2 & year_lic==2014 & metres<500
replace t=1 if year==2017 & publico==2 & year_lic==2015 & metres<500
replace t=1 if year==2017 & publico==2 & year_lic==2016 & metres<500
replace t=1 if year==2017 & publico==2 & year_lic==2017 & metres<500

*/

gen treatment=0
replace treatment=1 if metres<500 & year>year_lic & publico==2 

gen treatment_all=0
replace treatment_all=1 if metres<500 & year>year_lic


* This is to manually get data 'wide'
drop num
gen num=.
replace num= n18 if year==2017
replace num= n16 if year==2016
replace num= n15 if year==2015
replace num= n14 if year==2014

drop pau
gen pau=.
replace pau= pau1516 if year==2015
replace pau= pau16 if year==2016
replace pau= pau17 if year==2017
replace pau= pau14 if year==2014

* Income by district: 
drop renta
gen renta = .
replace renta = 31 if distrito==4
replace renta = 40 if distrito==1
replace renta = 51 if distrito==14
replace renta = 53 if distrito==15
replace renta = 60 if distrito==5
replace renta = 34 if distrito==17
replace renta = 48 if distrito==6
replace renta = 48 if distrito==8
replace renta = 56 if distrito==11
replace renta = 30 if distrito==10
replace renta = 27 if distrito==3
replace renta = 25 if distrito==18
replace renta = 24 if distrito==13
replace renta = 34 if distrito==12
replace renta = 36 if distrito==7
replace renta = 47 if distrito==9
replace renta = 26 if distrito==21
replace renta = 30 if distrito==20
replace renta = 31 if distrito==19
replace renta = 34 if distrito==16
replace renta = 46 if distrito==2

* Dummy variable for income: Districts with incomes higher to the median==1
drop renta2
gen renta2 = 0 
replace renta2 = 1 if renta>40

/* Variables and placebos about the treatment:

gen treatment_all=0
replace treatment_all=1 if metres<500 & year>year_lic

gen treatment_pl=0
replace treatment_pl=1 if metres<500 & year==year_lic

gen treatment_pl2=0
replace treatment_pl2=1 if metres<500 & year<year_lic

gen treatment_pl3=0
replace treatment_pl3=1 if metres<500 & year<year_lic-1

gen treatmentfut = 0
replace treatmentfut = 1 if metres<500 & year>year_lic+1

*/


*cd "~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"
cd "/Users/mariadelmarcanizaresespadafor/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates"


** Publico=1 -> charter schools
** Publico=2 -> public schools

** Renta2=0 -> poorer 
** Renta2=1 -> richer

*use workingfile.dta, clear

drop treat
gen treat=treatment_all

***********************
***********************

*** This makes stata understand data as a panel. And, when applying the xtreg command, adding fixed effects at the observation level. 

xtset school_id year

***********************
***********************

xtreg num treat i.year, fe
regsave treat using "num1.dta", tstat pval ci level(90) replace addlabel (Outcome, num,Income, All, School, All, Year, 1, Level, 90)

xtreg num treat i.year, fe
regsave treat using "num1.dta", tstat pval ci level(95) append addlabel (Outcome, num,Income, All, School, All, Year, 1, Level, 95)


*** Charter

xtreg num treat i.year if publico==1, fe
regsave treat using "num1.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, All, School, Charter, Year, 1, Level, 90)

xtreg num treat i.year if publico==1, fe
regsave treat using "num1.dta", tstat pval ci level(95) append addlabel (Outcome, num,Income, All, School, Charter, Year, 1, Level, 95)

***Public

xtreg num treat i.year if publico==2, fe
regsave treat using "num1.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, All, School, Public, Year, 1, Level, 90)

xtreg num treat i.year if publico==2, fe
regsave treat using "num1.dta", tstat pval ci level(95) append  addlabel (Outcome, num,Income, All, School, Public, Year, 1, Level, 95)

*** Charter, poor

xtreg num treat i.year if publico==1 & renta2==0, fe
regsave treat using "num1.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, Poor, School, Charter, Year, 1, Level, 90)

xtreg num treat i.year if publico==1 & renta2==0, fe
regsave treat using "num1.dta", tstat pval ci level(95) append addlabel (Outcome, num,Income, Poor, School, Charter, Year, 1, Level, 95)

*** Charter, rich

*xtreg num treat i.year if publico==1 & renta2==1, fe
*regsave treat using "num1.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, Rich, School, Charter, Year, 1, Level, 90)

*xtreg num treat i.year if publico==1 & renta2==1, fe
*regsave treat using "num1.dta", tstat pval ci level(95) append addlabel (Outcome, num,Income, Rich, School, Charter, Year, 1, Level, 95)

*** Public, poor

xtreg num treat i.year if publico==2 & renta2==0, fe
regsave treat using "num1.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, Poor, School, Public, Year, 1, Level, 90)

xtreg num treat i.year if publico==2 & renta2==0, fe
regsave treat using "num1.dta", tstat pval ci level(95) append addlabel (Outcome, num, Income, Poor, School, Public, Year, 1, Level, 95)

*** Public, rich

xtreg num treat i.year if publico==2 & renta2==1, fe
regsave treat using "num1.dta", tstat pval ci level(90) append addlabel (Outcome, num,Income, Rich, School, Public, Year, 1, Level, 90)

xtreg num treat i.year if publico==2 & renta2==1, fe
regsave treat using "num1.dta", tstat pval ci level(95) append addlabel (Outcome, num, Income, Rich, School, Public, Year, 1, Level, 95)

use "num1.dta", clear 

replace Year = 2

append using "num0.dta"

gen ci_lower90 = ci_lower if Level==90
gen ci_upper90 = ci_upper if Level==90
gen ci_lower95 = ci_lower if Level==95
gen ci_upper95 = ci_upper if Level==95

gen out = 0 if Outcome=="num"

gen inc = 0 if Income=="Poor"
replace inc = 1 if Income=="All" 
replace inc = 2 if Income=="Rich" 

gen charter = 0 if School=="Charter"
replace charter = 1 if School=="Public"
replace charter = 2 if School=="All"

save "num2.dta", replace 



